package com.city.java.abnormal_videos.service.impl;

import com.city.java.abnormal_videos.mapper.AbnormalVideoMapper;
import com.city.java.abnormal_videos.pojo.entity.AbnormalVideos;
import com.city.java.base.response.JsonResult;
import com.city.java.base.response.StatusCode;
import com.city.java.abnormal_videos.service.AbnormalVideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 异常视频服务实现类
 */
@Slf4j
@Service
public class AbnormalVideoServiceImpl implements AbnormalVideoService {

    @Autowired
    private AbnormalVideoMapper abnormalVideoMapper;

    @Override
    public JsonResult addAbnormalVideo(AbnormalVideos abnormalVideo) {
        log.debug("准备添加异常视频: {}", abnormalVideo);

        // 参数校验
        if (abnormalVideo == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常视频信息不能为空");
        }

        if (abnormalVideo.getAbnormalId() == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常记录ID不能为空");
        }

        if (abnormalVideo.getVideoUrl() == null || abnormalVideo.getVideoUrl().trim().isEmpty()) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "视频URL不能为空");
        }

        // 设置创建时间
        if (abnormalVideo.getCreatedAt() == null) {
            abnormalVideo.setCreatedAt(LocalDateTime.now());
        }

        try {
            abnormalVideoMapper.insertAbnormalVideo(abnormalVideo);
            log.info("异常视频添加成功: 异常视频ID={}", abnormalVideo.getAbnormalVideoId());
            return JsonResult.ok();
        } catch (Exception e) {
            log.error("添加异常视频失败: 异常记录ID={}, 视频URL={}", abnormalVideo.getAbnormalId(), abnormalVideo.getVideoUrl(), e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "添加异常视频失败");
        }
    }

    @Override
    public JsonResult getAbnormalVideoById(Long abnormalVideoId) {
        log.debug("根据ID查询异常视频: {}", abnormalVideoId);

        // 参数校验
        if (abnormalVideoId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常视频ID不能为空");
        }

        try {
            AbnormalVideos abnormalVideo = abnormalVideoMapper.selectById(abnormalVideoId);
            if (abnormalVideo != null) {
                log.info("查询异常视频成功: ID={}", abnormalVideoId);
                return JsonResult.ok(abnormalVideo);
            } else {
                log.warn("异常视频不存在: ID={}", abnormalVideoId);
                return new JsonResult(StatusCode.OPERATION_FAILED, "异常视频不存在");
            }
        } catch (Exception e) {
            log.error("查询异常视频失败: ID={}", abnormalVideoId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询异常视频失败");
        }
    }

    @Override
    public JsonResult getAbnormalVideosByAbnormalId(Long abnormalId) {
        log.debug("根据异常记录ID查询异常视频列表: {}", abnormalId);

        // 参数校验
        if (abnormalId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常记录ID不能为空");
        }

        try {
            List<AbnormalVideos> abnormalVideos = abnormalVideoMapper.selectByAbnormalId(abnormalId);
            log.info("查询异常记录视频列表成功: 异常记录ID={}, 视频数量={}", abnormalId, abnormalVideos.size());
            return JsonResult.ok(abnormalVideos);
        } catch (Exception e) {
            log.error("查询异常记录视频列表失败: 异常记录ID={}", abnormalId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询异常视频列表失败");
        }
    }

    @Override
    public JsonResult getAllAbnormalVideos() {
        log.debug("查询所有异常视频列表");

        try {
            List<AbnormalVideos> abnormalVideos = abnormalVideoMapper.selectAll();
            log.info("查询所有异常视频列表成功: 视频数量={}", abnormalVideos.size());
            return JsonResult.ok(abnormalVideos);
        } catch (Exception e) {
            log.error("查询所有异常视频列表失败", e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询异常视频列表失败");
        }
    }

    @Override
    public JsonResult deleteAbnormalVideoById(Long abnormalVideoId) {
        log.debug("根据ID删除异常视频: {}", abnormalVideoId);

        // 参数校验
        if (abnormalVideoId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常视频ID不能为空");
        }

        try {
            int result = abnormalVideoMapper.deleteById(abnormalVideoId);
            if (result > 0) {
                log.info("删除异常视频成功: ID={}", abnormalVideoId);
                return JsonResult.ok();
            } else {
                log.warn("删除异常视频失败，异常视频不存在: ID={}", abnormalVideoId);
                return new JsonResult(StatusCode.OPERATION_FAILED, "异常视频不存在");
            }
        } catch (Exception e) {
            log.error("删除异常视频失败: ID={}", abnormalVideoId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "删除异常视频失败");
        }
    }

    @Override
    public JsonResult deleteAbnormalVideosByAbnormalId(Long abnormalId) {
        log.debug("根据异常记录ID删除异常视频: {}", abnormalId);

        // 参数校验
        if (abnormalId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常记录ID不能为空");
        }

        try {
            int result = abnormalVideoMapper.deleteByAbnormalId(abnormalId);
            log.info("删除异常视频成功: 异常记录ID={}, 删除数量={}", abnormalId, result);
            return JsonResult.ok();
        } catch (Exception e) {
            log.error("删除异常视频失败: 异常记录ID={}", abnormalId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "删除异常视频失败");
        }
    }

    @Override
    public JsonResult updateAbnormalVideo(AbnormalVideos abnormalVideo) {
        log.debug("更新异常视频信息: {}", abnormalVideo);

        // 参数校验
        if (abnormalVideo == null || abnormalVideo.getAbnormalVideoId() == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常视频ID不能为空");
        }

        if (abnormalVideo.getVideoUrl() != null && abnormalVideo.getVideoUrl().trim().isEmpty()) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "视频URL不能为空");
        }

        try {
            // 检查异常视频是否存在
            AbnormalVideos existingAbnormalVideo = abnormalVideoMapper.selectById(abnormalVideo.getAbnormalVideoId());
            if (existingAbnormalVideo == null) {
                log.warn("更新异常视频失败，异常视频不存在: ID={}", abnormalVideo.getAbnormalVideoId());
                return new JsonResult(StatusCode.OPERATION_FAILED, "异常视频不存在");
            }

            int result = abnormalVideoMapper.updateById(abnormalVideo);
            if (result > 0) {
                log.info("更新异常视频成功: ID={}", abnormalVideo.getAbnormalVideoId());
                return JsonResult.ok();
            } else {
                log.warn("更新异常视频失败: ID={}", abnormalVideo.getAbnormalVideoId());
                return new JsonResult(StatusCode.OPERATION_FAILED, "更新异常视频失败");
            }
        } catch (Exception e) {
            log.error("更新异常视频失败: ID={}", abnormalVideo.getAbnormalVideoId(), e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "更新异常视频失败");
        }
    }
}
